using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.Web;
using ECommerce.Library;

public partial class login : SitePublicBasePage
{
    public String SupplierId
    {
        get
        {
            try
            {

                return Page.RouteData.Values["partnerId"].ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!String.IsNullOrEmpty(Request["ReturnUrl"]))
            SessionManager.ReturnUrl = Request["ReturnUrl"];
       
        if (!Page.IsPostBack)
        {
            ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

            Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
            Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

            if (litPageTitle != null)
            {
                litCurrentPage.Text = litPageTitle.Text = "Login";

            }

            hplForgotPassword.NavigateUrl = string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.Recover : "~/partner/" + SupplierId + "/recover.aspx";
            if (SessionManager.IsUserLoggedIn)
            {
                if (SessionManager.ReturnUrl != null)
                {
                   string returnUrl = SessionManager.ReturnUrl;
                    SessionManager.ReturnUrl = null;
                    Response.Redirect(returnUrl);
                }
                else
                {
                    Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.UserHome : "~/partner/" + SupplierId + "/myaccount/default.aspx");
                }
            }
            if (CookieManager.IsCookieExists(CookieManager.USER_NAME))
            {
                string emailAddress = CookieManager.GetCookieValue(CookieManager.USER_NAME);
                string password = CookieManager.GetCookieValue(CookieManager.PASSWORD);
                cbxRemember.Checked = true;
                LoginUser(emailAddress, password);
            }

        }
    }
    protected void LoginUser(string emailAddress, string password)
    {
        if (AutoGearSiteUser.Login(emailAddress, password, UserLocationType.FrontEnd))
        {
            if (cbxRemember.Checked)
            {
                CookieManager.SetCookieValue(CookieManager.USER_NAME, emailAddress, DateTime.Now.AddDays(SiteSettings.CookieTimeOut));
                CookieManager.SetCookieValue(CookieManager.PASSWORD, password, DateTime.Now.AddDays(SiteSettings.CookieTimeOut));
            }
            else
            {
                CookieManager.RemoveCookie(CookieManager.USER_NAME);
                CookieManager.RemoveCookie(CookieManager.PASSWORD);
            }

            if (SessionManager.ReturnUrl != null)
            {
                string returnUrl = SessionManager.ReturnUrl;
                SessionManager.ReturnUrl = null;
                Response.Redirect(returnUrl);
            }
            else
            {
                Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.UserHome : "~/partner/" + SupplierId + "/myaccount/default.aspx");
            }
        }
        else
        {
            throw new Exception();

        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string emailAddress = tbxEmailAddress.Text.Trim();
            string password = tbxPassword.Text.Trim();
            if (RegExUtil.IsEmail(emailAddress) && RegExUtil.IsPassword(password))
            {
                LoginUser(emailAddress, password);
            }
            else
            {
                throw new Exception();
            }
        }
        catch (Exception ex)
        {
            uccResponseMessage.SetFailMessage("Sign In failed. Please try again");
            uccResponseMessage.Visible = true;
        }
    }
    protected void btnCreateAccount_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.Register : "~/partner/" + SupplierId + "/register.aspx");
    }
    protected void btnCheckOut_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.ShoppingCart : "~/partner/" + SupplierId + "/shopping-cart.aspx");
    }
}
